<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div v-show="isShowSeach" class="container mt-1">
          <el-form inline>
            <el-form-item label="推广用户：">
              <el-input v-model="tableFrom.userId" placeholder="请输入推广用户ID" class="selWidth" size="small" clearable />
            </el-form-item>
            <el-form-item label="采购商：">
              <el-input v-model="tableFrom.merchantUserId" placeholder="请输入采购商ID" class="selWidth" size="small" clearable />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="getList(1)">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-button size="small" type="primary" @click="onAdd" v-hasPermi="['platform:user:brokerage:config:save']"
          >新增返佣配置</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
      >
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column label="推广用户" min-width="150">
          <template slot-scope="scope">
            <div>{{scope.row.userName}}</div>
            <div>{{scope.row.userPhone}}</div>
          </template>
        </el-table-column>
        <el-table-column label="采购商" min-width="150">
          <template slot-scope="scope">
            <div>{{scope.row.merchantUserName}}</div>
            <div>{{scope.row.merchantUserPhone}}</div>
          </template>
        </el-table-column>
        <el-table-column label="一级返佣比例" prop="firstRatio" min-width="100">
          <template slot-scope="scope">
            {{scope.row.firstRatio}}%
          </template>
        </el-table-column>
        <el-table-column label="二级返佣比例" prop="secondRatio" min-width="100">
          <template slot-scope="scope">
            {{scope.row.secondRatio}}%
          </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="updateTime" min-width="150" />
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row)" 
              v-hasPermi="['platform:user:brokerage:config:update']">编辑</el-button>
            <el-button type="text" size="small" 
              v-hasPermi="['platform:user:brokerage:config:delete']"
              @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <createUserBrokerage ref="createConfig" :editData="editData" @getList="getList" />
  </div>
</template>

<script>
import createUserBrokerage from './createUserBrokerage'
import { brokerageConfigListApi, brokerageConfigDeleteApi } from '@/api/distribution'

export default {
  name: 'UserBrokerageConfigList',
  components: { createUserBrokerage },
  data() {
    return {
      listLoading: false,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        userId: '',
        merchantUserId: ''
      },
      editData: {},
      isShowSeach: true
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num || this.tableFrom.page
      brokerageConfigListApi({
        pageNo: this.tableFrom.page,
        pageSize: this.tableFrom.limit,
        userId: this.tableFrom.userId || null,
        merchantUserId: this.tableFrom.merchantUserId || null
      }).then(res => {
        this.tableData.data = res.list
        this.tableData.total = res.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleDelete(id) {
      this.$modalSure('确定要删除该配置吗?').then(() => {
        brokerageConfigDeleteApi(id).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    onAdd() {
      this.editData = {}
      this.$refs.createConfig.dialogVisible = true
    },
    onEdit(row) {
      this.editData = row
      this.$refs.createConfig.dialogVisible = true
    },
    handleDelete(id) {
      this.$modalSure('确定要删除该配置吗?').then(() => {
        this.$http.get(`api/admin/platform/user/brokerage/config/delete/${id}`).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.selWidth {
  width: 300px !important;
}
</style>
